<template>
	<view class="">
		<web-view @message="handleMessage" :src="src" :update-title="false"></web-view>
	</view>
</template>

<script>
	import {
		sub,
	} from '../../common/event'
	import config from '../../config'
	import moment from 'moment'

	export default {
		data() {
			return {
				src: '',
				gid: '',
				fid: '',
				name: ''
			}
		},
		onBackPress(options) {
			console.log(147, options)
			// this.sendMessage({
			// 	type: "backpress",
			// 	payload: {}
			// })
			// return true
		},
		onLoad({
			fid,
			gid,
			name
		}) {
			this.fid = fid
			this.gid = gid
			this.name = name
			this.src = "/hybrid/html/uploadfile.html?url=" + encodeURIComponent(config.hostUrl)
			const _this = this
			let currentWebview = this.$scope.$getAppWebview();
			setTimeout(() => {
				this.wv = currentWebview.children()[0]
			}, 200)
		},
		onUnload() {

		},
		methods: {

			socketMessage: function(e) {
				const _this = this
				console.log(123123, e)

			},
			sendMessage(data) {
				const message = JSON.stringify(data)
				this.wv.evalJS(`app.receviceMessage(${message})`);
			},
			handleMessage: function(data) {
				const _this = this
				const message = data.detail.data[0]
				console.log(888888, message)
				if (message.type == "upsuccess") {
					const ret = message.payload
					this.sendfilemsg(ret)
				}
				if (message.type == "uperror") {
					uni.showToast({
						title: "上传失败",
						icon: "none"
					})
					uni.navigateBack()
				}
			},
			sendfilemsg: async function({str, name, size}) {
				console.log(8888881, str, name, size)
				
				const user = this.$store.state.user.user
				if (this.gid > 0) {
					const data = {
						name: user.name,
						uid: user.id,
						toname: '',
						toid: 0,
						gid: this.gid,
						gname: this.name,
						type: 'file',
						content: `${str}`,
						timelong: size ? Math.floor(size * 1) : 0,
						time: moment().valueOf(),
						money: 0,
						status: '正常',
						note: name
					}
					const ret = await this.$u.post(`chat/chat/sendGMessage`, data)
					data.id = ret.data
					console.log(ret)
					this.$store.commit(`message/addMessage`, data)
					uni.navigateBack()
				} else {
					const data = {
						name: user.name,
						uid: user.id,
						toname: this.name,
						toid: this.fid,
						gid: 0,
						gname: '',
						type: 'file',
						content: `${str}`,
						timelong: size ? Math.floor(size * 1) : 0,
						time: moment().valueOf(),
						money: 0,
						status: '正常',
						note: name
					}
					console.log(8888883, data)
					const ret = await this.$u.post(`chat/chat/sendMessage`, data)
					data.id = ret.data
					console.log(ret)
					this.$store.commit(`message/addMessage`, data)
					uni.navigateBack()
				}

			}
		}
	}
</script>

<style>
</style>